package com.zhaoyan.okgorxjavademo.base;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by cxy on 2017/11/16.
 */

public abstract class BasePresenterFragment<T extends BasePresenter> extends Fragment {
    protected View view;
    private Unbinder unbinder;
    @Inject
    protected T mPresenter;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(getLayout(), container, false);
        unbinder = ButterKnife.bind(this, view);
        sp = getActivity().getSharedPreferences("config", MODE_PRIVATE);
        editor = sp.edit();
        initInject();
        initView();
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        mPresenter.detachView();
    }

    protected abstract void initView();

    protected abstract int getLayout();
    protected abstract void initInject();
    public void putString(String value, String key) {
        editor.putString(value, key);
        editor.commit();
    }

    public void putInt(String value, int key) {
        editor.putInt(value, key);
        editor.commit();
    }
    public void putBoolean(String key,boolean value) {
        editor.putBoolean(key,value);
        editor.commit();
    }

    public String getString(String value) {
        return sp.getString(value, "");
    }

    public int getInt(String value) {
        return sp.getInt(value, 0);
    }
    public boolean getBoolean(String key) {
        return sp.getBoolean(key,false);
    }
}
